package broker

import (
	"fmt"
	"honeyweb/log"
	"strings"
)

const (
	ANY_UID = 0 //代表所有的用户id
	//通知给用户的消息
	EVENT_GATE_AGENT_MSG_PREFIX = "EVENT.gate.agent.msg.%d"

	// 玩家连接上gate
	EVENT_GATE_AGENT_CONNECT = "EVENT.gate.agent.connect"
	// 玩家断开 gate
	EVENT_GATE_AGENT_DISCONNECT = "EVENT.gate.agent.disconnect"

	//钱包数值变化
	EVENT_WALLET_CHANGE_GOLD = "EVENT.wallet.change.gold"
	EVENT_WALLET_CHANGE_DIAMOND = "EVENT.wallet.change.diamond"
	EVENT_WALLET_CHANGE_SCORE = "EVENT.wallet.change.score"
)

const (
	RPC_ACCOUNT_PREFIX = "RPC.account.%d"
)

var SubjectMap map[string]string

//初始化所有的service-subject的对应关系
func InitSubjects() {
	SubjectMap = make(map[string]string)
	RegisterServiceSubject("account", AccountRPCSubject())
	RegisterServiceSubject("wallet", WalletRPCSubject())
	RegisterServiceSubject("status", UserStatusRPCSubject())
}

func RegisterServiceSubject(service, subject string){
	SubjectMap[service] = subject
}


//推送给玩家消息的 subject
func UserAgentEvent(uid int) string{
	return fmt.Sprintf(EVENT_GATE_AGENT_MSG_PREFIX, uid)
}

//账号系统的 subject
func AccountRPCSubject() string {
	return fmt.Sprintf(RPC_ACCOUNT_PREFIX, 0)
}

//钱包系统的 subject
func WalletRPCSubject() string {
	return "RPC.wallet.0"
}
//用户状态系统的 subject
func UserStatusRPCSubject() string {
	return "RPC.userstatus.0"
}

func GetSubjectByMethod(method string) string {
	service := GetServiceFromMethod(method)

	subject, ok := SubjectMap[service]
	if ok {
		return subject
	} else {
		log.S().Errorf("not found service:%s subject", service)
		return ""
	}
}

//拆分 method
func SplitMethod(method string) (string, string) {
	strs := strings.SplitN(method,".", 2)
	if len(strs) == 2 {
		return strs[0], strs[1]
	}
	log.S().Errorf("error method: %s", method)
	return "", ""
}

//获取method中的服务名
func GetServiceFromMethod(method string) string {
	service, _ := SplitMethod(method)
	return service
}
//获取 method 中的 处理 cmd
func GetCmdFromMethod(method string) string {
	_, cmd := SplitMethod(method)
	return cmd
}

